package com.cn.shuangzi.tt;

import com.bytedance.sdk.dp.DPWidgetDrawParams;
import com.bytedance.sdk.dp.IDPWidget;
import com.cn.shuangzi.SZApp;
import com.cn.shuangzi.ad.ADManager;

/**
 * Created by CN.
 */
public abstract class DPApp extends SZApp implements ADManager.OnAdInitDoneListener {
    protected IDPWidget mIDPWidget;

    protected boolean isInitDoneShowTTADContent;

    protected boolean isShowTTADContent;

    protected void initDrawWidget() {
        if (mIDPWidget == null) {
            synchronized (DPApp.class) {
                if (mIDPWidget == null) {
                    if (DPManager.getInstance().isInitSuccess()) {
                        try {
                            mIDPWidget = DPManager.getInstance().buildDrawWidget(DPWidgetDrawParams.obtain()
                                    .adOffset(49) //单位 dp
                                    .hideClose(true, null)
                            );
                        } catch (Exception e) {
                            mIDPWidget = null;
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    public IDPWidget getIDPWidget() {
        return mIDPWidget;
    }

    public boolean isShowTTADContent(){
        if(isInitDoneShowTTADContent){
            return isShowTTADContent;
        }
        isShowTTADContent = mIDPWidget != null && ADManager.getInstance().isShowAd();
        isInitDoneShowTTADContent = true;
        return isShowTTADContent;
    }

    public void setInitDoneShowTTADContent(boolean initDoneShowTTADContent) {
        isInitDoneShowTTADContent = initDoneShowTTADContent;
    }

    @Override
    public void onTTAdInitDone(boolean isSuccess) {
        if(getAppIdTTContent()!=null&&getSDKSettingFileName()!=null) {
            if (mIDPWidget == null) {
                DPManager.getInstance().init(this, getAppIdTTContent(), getSDKSettingFileName(), new OnDPSdkInitListener() {
                    @Override
                    public void onInitDone(boolean isSuccess) {
                        if (isSuccess) {
                            if(isInitDrawWidget()) {
                                initDrawWidget();
                            }
                        }
                    }
                });
            }
        }
    }
    public abstract String getAppIdTTContent();
    public abstract String getSDKSettingFileName();
    public abstract boolean isInitDrawWidget();
}
